package cn.edu.qzuie.artist.module.td.controller.admin.major;

import cn.edu.qzuie.artist.module.td.controller.admin.department.vo.DepartmentForSelectVo;
import org.springframework.web.bind.annotation.*;

import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.edu.qzuie.artist.framework.common.pojo.PageParam;
import cn.edu.qzuie.artist.framework.common.pojo.PageResult;
import cn.edu.qzuie.artist.framework.common.pojo.CommonResult;
import cn.edu.qzuie.artist.framework.common.util.object.BeanUtils;
import static cn.edu.qzuie.artist.framework.common.pojo.CommonResult.success;

import cn.edu.qzuie.artist.framework.excel.core.util.ExcelUtils;

import cn.edu.qzuie.artist.framework.apilog.core.annotation.ApiAccessLog;
import static cn.edu.qzuie.artist.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.edu.qzuie.artist.module.td.controller.admin.major.vo.*;
import cn.edu.qzuie.artist.module.td.dal.dataobject.major.MajorDO;
import cn.edu.qzuie.artist.module.td.service.major.MajorService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - TD专业")
@RestController
@RequestMapping("/td/major")
@Validated
public class MajorController {

    @Resource
    private MajorService majorService;

    @PostMapping("/create")
    @Operation(summary = "创建专业")
    @PreAuthorize("@ss.hasPermission('td:major:create')")
    public CommonResult<Long> createMajor(@Valid @RequestBody MajorSaveReqVO createReqVO) {
        return success(majorService.createMajor(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新专业")
    @PreAuthorize("@ss.hasPermission('td:major:update')")
    public CommonResult<Boolean> updateMajor(@Valid @RequestBody MajorSaveReqVO updateReqVO) {
        majorService.updateMajor(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除专业")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('td:major:delete')")
    public CommonResult<Boolean> deleteMajor(@RequestParam("id") Long id) {
        majorService.deleteMajor(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得专业")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('td:major:query')")
    public CommonResult<MajorRespVO> getMajor(@RequestParam("id") Long id) {
        MajorDO major = majorService.getMajor(id);
        return success(BeanUtils.toBean(major, MajorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得专业分页")
    @PreAuthorize("@ss.hasPermission('td:major:query')")
    public CommonResult<PageResult<MajorRespVO>> getMajorPage(@Valid MajorPageReqVO pageReqVO) {
        PageResult<MajorDO> pageResult = majorService.getMajorPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MajorRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出专业 Excel")
    @PreAuthorize("@ss.hasPermission('td:major:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMajorExcel(@Valid MajorPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MajorDO> list = majorService.getMajorPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "专业.xls", "数据", MajorRespVO.class,
                        BeanUtils.toBean(list, MajorRespVO.class));
    }
    @GetMapping("/grtSchoolAndDepartmentTree")
    @Operation(summary = "获得二级树结构数据，数据包括学校数据和所属部门数据")
    @PreAuthorize("@ss.hasPermission('td:major:query')")
    public CommonResult<List<SchoolDepartForTreeVo>> getSchoolAndDepartmentTree(){

        return success(majorService.createSchoolDepartmentTree());
    }

    @GetMapping("/getMajorAndDepartNameRespVoPage")
    @Operation(summary = "获得专业和部门名称分页")
    @PreAuthorize("@ss.hasPermission('td:major:query')")
    public CommonResult<PageResult<MajorAndDepartNameRespVo>> getMajorAndDepartNameRespVoPage(@Valid MajorPageReqVO pageReqVO) {
        PageResult<MajorAndDepartNameRespVo> pageResult = majorService.getMajorAndDepartNameRespVoPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MajorAndDepartNameRespVo.class));
    }

    @GetMapping("/getMajorsBySchoolId")
    @Operation(summary = "根据学校编号获得专业")
    @Parameter(name = "schoolId", description = "编号", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('td:major:query')")
    public CommonResult<List<MajorForSelectVo>> getMajorsBySchoolId(@RequestParam("schoolId") Long id){
        return  success(majorService.getMajors(id)) ;
    }

}